/**
 * Pearlsen Software 2011
 * 
 * Creator:haitingr
 * 
 */
var __context__='/';
var __query_uri='/query.asp';
var __dataset_uri='/dataset.asp';
var __json_param='_json_message';

// Condition
function Condition(name,value){
	this.fieldName=name;
	this.value=value;
}

Condition.prototype.getName=function(){
	return this.fieldName;
};

Condition.prototype.getValue=function(){
	return this.value;
};

// Query
function Query(handlerName, datasetName) {
	this.handler = handlerName;
	this.datasetName = datasetName;
	this.conditions = [];
	this.start=0;
	this.pageSize=15;
	this.handlerMethod="handler";
	this.url = '/query.asp';
};

Query.prototype.setURL=function(url){
	this.url=url;
};
Query.prototype.setForm=function(form){
	this.form=form;
};
Query.prototype.setHandlerMethod=function(method){
	this.handlerMethod=method;
};

Query.prototype.setHandler=function(handler){
	this.handler=handler;
};

Query.prototype.setDataSetName=function(datasetName){
	this.datasetName=datasetName;
};

Query.prototype.addCondition=function(name,value){
	var condition=new Condition(name,value);
	this.conditions.push(condition);
};
Query.prototype.setConditions=function(conditions){
	conditions = conditions || [];
	this.conditions= conditions;
};


Query.prototype.setStart=function(start){
	this.start=start;
};

Query.prototype.setPageSize=function(pageSize){
	this.pageSize=pageSize;
};

//Query.prototype.setErrorFun = function (errorFun){
//	this.errorFun = errorFun;
//};
Query.prototype.setCompletedFun = function (completedFun){
	this.completedFun = completedFun;
};
Query.prototype.setCompletedFunScope=function(scope){
	this.completedFunScope = scope;
};
//Query.prototype.setSuccessFun = function (SuccessFun){
//	this.successFun = SuccessFun;
//};

Query.prototype.toJson = function() {
	var json ={};
	json.handler = this.handler;
	json.datasetName = this.datasetName;
	json.conditions = this.conditions;
	json.start= this.start;
	json.pageSize= this.pageSize;
	json.handlerMethod=this.handlerMethod;
	return Ext.JSON.encode(json);
};

Query.prototype.load = function(p){
	 Ext.MessageBox.show({
           title: '请稍等',
           msg: '数据加载中...',
           progressText: '数据加载中...',
           width:300,
           progress:true,
           closable:false
       });
	var me = this;
	if(p&&p.params){
		$(p.params).each(function(i, item) {
			me.addCondition(item['name'],item['value']);
        });
	}
	var ds=null;
	var json= me.toJson();
	var url=Pls.Webpath+this.url;
	Ext.Ajax.request({
		 url:url,
		 method:"POST",
		 async:false,
		 params:{
			_json_message:json
		 },
		 success:function(response, opts){
			 Ext.MessageBox.hide();
			ds=new DataSet();
		    ds.fromJsonObject(Ext.JSON.decode(response.responseText));
		    if( typeof me.completedFun=='function' ) {
		    	var scope = me.completedFunScope ? me.completedFunScope:this;
		    	me.completedFun.call(scope,true,ds,response, opts);
		    	//me.completedFun(true,ds,response, opts);
		    }

		 },
		 failure: function(response, opts) {
			 Ext.MessageBox.hide();	
			 if( typeof me.completedFun=='function' ) {
				var scope = me.completedFunScope ? me.completedFunScope:this;
			    me.completedFun.call(scope,false,null,response, opts);
			 }
			 //if(me.completedFun) me.completedFun(false,null,response, opts);		 
		 }
	});
	return ds;
};
Query.prototype.reload= function(){
	this.load();
};
Query.prototype.loadNext= function(){
	this.start = this.start+1;
	this.load();
};
Query.prototype.loadPrev = function(){
	this.start = this.start-1;
	this.load();
};
Query.prototype.loadFirst = function(){
	this.start = 0;
	this.load();
};
Query.prototype.loadLast = function(){
	this.start =  this.total-1;
	this.load;
};


// Datset Definition

function Record(){
	//this.values={};
	this.status = 0;// new:0|edit:1|detele:2
	this.key;
}
Record.prototype.setKey = function (key){
	this.key = key;
};

Record.prototype.setStatus = function (status){
	this.status = status;
};
Record.prototype.setValue=function(name,value){
	this[name]=value;
};

Record.prototype.getValue=function(name){
	return this[name];
};

Record.prototype.toString=function(){
	var temp="";
	$.each(this.values,function(key,value){
		temp+=''+$.quoteString(key)+':';
		if(value instanceof DataSet){
			temp+=value.toJson();
		}else{
			temp+=''+$.quoteString((''+value))+',';
		}
	});
	return temp.substring(0, temp.length-1);
};


// Field
function Field(name,type,length){
	this.name=name;
	this.type=type;
	this.length=length;
}

Field.prototype.setType=function(type){
	this.type=type;
};

Field.prototype.setLength=function(length){
	this.length=length;
};

Field.prototype.getName=function(){
	return this.name;
};

Field.prototype.getType=function(){
	return this.type;
};

Field.prototype.getLength=function(){
	return this.length;
};

//
function DataSetEvent(){
	this.index=0;
	this.fieldname=null;
	this.oldValue=null;
	this.newValue=null;
}


// DataSet
function DataSet(handler,name){
	this.name=name;
	this.handler=handler;
	this.record=[];
	this.condition=[];
	this.index=-1;
	this.fields=[];
	this.page ={};
	this.page.pagesize=10;
	this.page.total=0;
	this.page.start=0;
	this.handlerMethod="handler";
	this.url = "/dataset.asp";
}
DataSet.prototype.setURL=function(url){
	this.url=url;
};
DataSet.prototype.setHandlerMethod=function(method){
	this.handlerMethod=method;
};

DataSet.prototype.addCondition=function(name,value){
	var condition=new Condition(name,value);
	this.condition.push(condition);
};

DataSet.prototype.getPageSize=function(){
	return this.page.pagesize;
};

DataSet.prototype.getTotal=function(){
	return this.page.total;
};

DataSet.prototype.getStart=function(){
	return this.page.start;
};

DataSet.prototype.setFields=function(fields){
	this.fields=fields;
};

DataSet.prototype.addField=function(name,type,length){
	var f=new Field(name,type,length);
	this.fields.push(f);
};

DataSet.prototype.fieldCount=function(){
	return this.fields.length;
};

DataSet.prototype.getField=function(index){
	return this.fields[index];
};

DataSet.prototype.getFieldByName=function(name){
	for(var i=0;i<this.fields.length;i++){
		if(this.fields[i].getName().equals(name)){
			return this.fields[i];
		}
	}
	return null;
};

DataSet.prototype.setHandler=function(handler){
	this.handler=handler;
};

DataSet.prototype.size=function(){
	return this.record.length;
};

DataSet.prototype.move=function(index){
	this.index=index;
};

DataSet.prototype.currentIndex=function(){
	return index;
};

DataSet.prototype.newRecord=function(){
	this.record.push(new Record());
	this.index=this.size()-1;
};
DataSet.prototype.setRecord = function(record){
	this.record.push(record);
	this.index=this.size()-1;
};
DataSet.prototype.first=function(){
	this.index=0;
};

DataSet.prototype.last=function(){
	this.index=this.size()-1;
};

DataSet.prototype.next=function(){
	if((this.index+1)>=this.size()){
		return false;
	}
	this.index++;
	return true;
};

DataSet.prototype.clear=function(){
	this.index=-1;
	this.record=[];
};

DataSet.prototype.getValue=function(name){
	return this.getCurrent().getValue(name);
};

DataSet.prototype.getCurrent = function(){
	return this.record[this.index];
}
DataSet.prototype.setValue=function(name,value){
	this.getCurrent().setValue(name,value);
};

DataSet.prototype.fromJsonObject=function(obj){
	this.name=obj["name"];
	//
	var record=obj["record"];
	var nowds=this;
	for(var i=0;i<record.length;i++){
		this.newRecord();
		Ext.iterate(record[i],function(key, value, myself){
			
//			if(Object.prototype.toString.call(value) == '[object Array]'){
//				var temp=new DataSet();
//				temp.fromJson(value);
//				nowds.setValue(key, temp);
//			}else{
//				nowds.setValue(key,value);
//			}
			nowds.setValue(key,value);
		});
	}
	//
	var fs=obj["field"];
	if(fs != undefined){
		for(var i;i<fs.length;i++){
			this.addField(fs[i]["name"], fs[i]["type"], fs[i]["length"]);
		}
	}
	//
	this.page=obj["page"];
	this.pagesize=this.page["pagesize"];
	this.total=this.page["total"];
	this.start=this.page["start"];
};

DataSet.prototype.fromJson=function(json){
	var obj=eval("("+json+")");
	this.fromJsonObject(obj);
};

DataSet.prototype.toJson=function(){
	return Ext.JSON.encode(this);
};


DataSet.prototype.execute = function(resultCallback){
	if(resultCallback == undefined || resultCallback == null){
		resultCallback = function(result){
			if(result.getStatus()=="200"){
				Pearlsen.Utils.showSuccessMsg('操作成功!');
				return;
			}else{
				Pearlsen.Utils.showErrorMsg('操作失败!');
				return ;
			}
		};
	}
	var result=null;
	var json=this.toJson();
	 
	var url=Pls.Webpath+this.url+'?'+__json_param+'='+json;
	Ext.Ajax.request({
		method:'post',
		url:url,
		async: false,
		success: function(response){
			result=new Result();
			result.fromJsonObject(Ext.JSON.decode(response.responseText));
	        resultCallback(result);
	    }
	});
	return result;
};

// Result

function Result(){
	this.status="200";
	this.message="success";
	this.success=true;
	this.records=null;
}

Result.prototype.setSuccess=function(success){
	this.success=success;
};

Result.prototype.getSuccess=function(){
	return this.success;
};

Result.prototype.setStatus=function(code){
	this.status=code;
};

Result.prototype.getStatus=function(){
	return this.status;
};

Result.prototype.getMessage=function(){
	return this.message;
};

Result.prototype.setMessage=function(message){
	this.message=message;
};
Result.prototype.getRecords=function(){
	return this.records;
};

Result.prototype.setRecords=function(records){
	this.records=records;
};
Result.prototype.fromJsonObject=function(obj){
	this.status=obj["status"];
	this.message=obj["message"];
	this.success=obj["success"];
	this.records=obj["records"];
};
